package com.gzx.plugin.tjzy.modular.call.vos.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.gzx.plugin.tjzy.modular.call.vos.resp.GzxVosAccessGatewayResp;
import com.gzx.plugin.tjzy.modular.call.vos.service.GzxVosAccessGatewayDbService;
import com.gzx.plugin.tjzy.modular.call.vos.service.GzxVosAccessGatewayService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.cache.CommonRedisUtil;
import vip.xiaonuo.common.sses.consts.SseMsgTypeConstant;
import vip.xiaonuo.common.sses.entity.SseMessage;
import vip.xiaonuo.common.sses.util.CommonSseUtil;
import vip.xiaonuo.common.util.CommonHttpUtil;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zjt
 * @description
 * @date 2025/5/16 16:35
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GzxVosAccessGatewayServiceImpl implements GzxVosAccessGatewayService {

    private final CommonSseUtil sseUtil;

    private final CommonRedisUtil redisUtil;

    private final GzxVosAccessGatewayDbService vosAccessGatewayDbService;

    @Value("${vos.url}")
    private String vosUrl;

    private final String REDIS_KEK = "current-call-size";

    /**
     * 获取线路当前并发数
     */
    @Override
    public void getCurrentCallSizeXxlJob() {
        //对接网关
        String vosRespStr = CommonHttpUtil.post(vosUrl + "GetGatewayMappingOnline", null, JSON.toJSONString(Map.of("", "")), "获取VOS网关数据");
        JSONObject jsonObject = JSON.parseObject(vosRespStr);
        if (jsonObject != null) {
            int retCode = jsonObject.getIntValue("retCode");
            if (retCode == 0) {
                List<GzxVosAccessGatewayResp> vosAccessGatewayDtoList = JSON.parseObject(
                        jsonObject.getString("infoGatewayMappingOnlines"),
                        new TypeReference<List<GzxVosAccessGatewayResp>>() {
                        });
                if (CollectionUtil.isNotEmpty(vosAccessGatewayDtoList)) {
                    List<GzxVosAccessGatewayResp> newVosAccessGatewayDtoList = vosAccessGatewayDtoList.stream().filter(s -> isSave(s.getName())).toList();
                    //保存到数据库
                    Thread.startVirtualThread(() -> vosAccessGatewayDbService.saveBatch(newVosAccessGatewayDtoList));
                    redisUtil.setCacheString(REDIS_KEK, JSON.toJSONString(newVosAccessGatewayDtoList));
                    sseUtil.sendMessageToAll(new SseMessage(SseMsgTypeConstant.CURRENT_CALL_SIZE, JSON.toJSONString(buildMessage(newVosAccessGatewayDtoList))));
                }
            }
        }
    }

    /**
     * 查询当前并发数
     *
     * @return
     */
    @Override
    public String getCurrentCallSize() {
        return (String) redisUtil.get(REDIS_KEK);
    }

    private boolean isSave(String name) {
        return (name.contains("移动") || name.contains("联通") || name.contains("电信")) && !name.contains("呼入") && !name.contains("上海唯渡") && !name.contains("认证");
    }

    private Map<String, Integer> buildMessage(List<GzxVosAccessGatewayResp> vosAccessGatewayDtoList) {
        return vosAccessGatewayDtoList.stream()
                .collect(Collectors.groupingBy(
                        GzxVosAccessGatewayResp::getName,
                        Collectors.summingInt(GzxVosAccessGatewayResp::getCurrentCall)));
    }
}
